package cn.exrick.manager.service.impl;

import cn.exrick.common.pojo.DataTablesResult;
import cn.exrick.manager.mapper.TbItemMapper;
import cn.exrick.manager.mapper.TbStockMapper;
import cn.exrick.manager.pojo.TbItem;
import cn.exrick.manager.pojo.TbStock;
import cn.exrick.manager.pojo.TbStockExample;
import cn.exrick.manager.service.StockService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class StockServiceImpl implements StockService {

    @Autowired
    private TbStockMapper tbStockMapper;
    @Autowired
    private TbItemMapper tbItemMapper;

    @Override
    public TbStock getStockById(Long id) {
        TbStock tbStock = tbStockMapper.selectByPrimaryKey(id);
        TbItem tbItem = tbItemMapper.selectByPrimaryKey(tbStock.getItemId());
        tbStock.setItemTitle(tbItem.getTitle());
        return tbStock;
    }

    @Override
    public void addStock(TbStock tbStock) {
        tbStock.setCreateTime(new Date());
        tbStock.setUpdateTime(new Date());
        tbStockMapper.insert(tbStock);
        //更新商品库存
        TbItem old = tbItemMapper.selectByPrimaryKey(tbStock.getItemId());
        TbItem tbItem = new TbItem();
        tbItem.setId(old.getId());
        tbItem.setNum(old.getNum()+tbStock.getNum());
        tbItemMapper.updateByPrimaryKeySelective(tbItem);
    }

    @Override
    public void updateStock(TbStock tbStock) {
        TbStock oldStock = tbStockMapper.selectByPrimaryKey(tbStock.getId());
        tbStock.setCreateTime(oldStock.getCreateTime());
        tbStock.setUpdateTime(new Date());
        tbStockMapper.updateByPrimaryKeySelective(tbStock);
        //更新商品库存
        TbItem oldItem = tbItemMapper.selectByPrimaryKey(tbStock.getItemId());
        TbItem tbItem = new TbItem();
        tbItem.setId(oldItem.getId());
        tbItem.setNum(oldItem.getNum() - oldStock.getNum() + tbStock.getNum());
        tbItemMapper.updateByPrimaryKeySelective(tbItem);
    }

    @Override
    public void deleteStock(List<Long> stockIds) {
        //更新商品库存
        for(Long id : stockIds){
            TbStock tbStock = tbStockMapper.selectByPrimaryKey(id);
            TbItem oldItem = tbItemMapper.selectByPrimaryKey(tbStock.getItemId());
            TbItem tbItem = new TbItem();
            tbItem.setId(oldItem.getId());
            tbItem.setNum(oldItem.getNum() - tbStock.getNum());
            tbItemMapper.updateByPrimaryKeySelective(tbItem);
        }
        TbStockExample example = new TbStockExample();
        TbStockExample.Criteria criteria = example.createCriteria();
        criteria.andIdIn(stockIds);
        tbStockMapper.deleteByExample(example);
    }

    @Override
    public DataTablesResult getStockList(int draw, int start, int length, TbStock tbStock, String orderCol, String orderDir) {
        DataTablesResult result=new DataTablesResult();
        PageHelper.startPage(start/length+1,length);
        List<TbStock> tbStocks = tbStockMapper.selectStockByCondition(tbStock, orderCol, orderDir);
        PageInfo<TbStock> pageInfo=new PageInfo<>(tbStocks);
        result.setRecordsFiltered((int)pageInfo.getTotal());
        result.setRecordsTotal((int)pageInfo.getTotal());
        result.setDraw(draw);
        result.setData(tbStocks);
        return result;
    }
}
